package com.taoyun.mapper;

import com.taoyun.pojo.entity.Group;
import com.taoyun.pojo.entity.User;
import com.taoyun.pojo.vo.UserVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *  User DAO 示例
 *
 * @author FengJingdong
 */
@Repository
public interface UserMapper {

    /**
     * 登录
     * @param userNumber 员工工号
     * @param password 密码
     * @return User 对象
     */
    @Select("select * from user where User_Number = #{userNumber} and Password = #{password}")
    @ResultType(User.class)
    User login(@Param("userNumber") String userNumber,@Param("password") String password) ;


    /**
     *  通过账号以及邮箱查找用户信息
     * @param userNumber 账号
     * @param email 邮箱
     * @return
     */
    @Select("select * from user where User_Number = #{userNumber} and Email = #{email}")
    @ResultType(User.class)
    User searchByEmailAndUserNumber(@Param("userNumber") String userNumber,@Param("email") String email);

    /**
     * 通过账号、密码对该账户密码进行重置
     * @param userNumber 账号
     * @param email 邮箱
     * @param newPassword 新密码
     * @return
     */
    @Update("update user set Password = #{newPassword} where User_Number = #{userNumber} and Email = #{email}")
    boolean resetPassword(@Param("userNumber") String userNumber,@Param("email") String email,@Param("newPassword") String newPassword);

    /**
     * 登录之后修改密码
     * @param userNumber
     * @param newPassword
     * @return
     */
    @Update("update user set Password = #{newPassword} where User_Number = #{userNumber}")
    boolean modifyPassword(@Param("userNumber") String userNumber,@Param("newPassword") String newPassword);

    /**
     * 登录后修改邮箱
     * @param userNumber
     * @param email
     * @return
     */
    @Update("update user set Email = #{email} where User_Number = #{userNumber} ")
    boolean updateEmail(@Param("userNumber") String userNumber,@Param("email") String email);


    //批量的在UserMapper，简单的直接注解操作
    //批量添加员工（XML配置）
//    int AddStaffsBatch(@Param("users") List<User> users);

    //批量删除员工（XML配置）
//    Integer DeleteStaffsBatch(@Param("ids") List<Integer> ids);



    //根据ID查找员工
//    @Select("select * from user where User_Id = #{id}")
//    @ResultType(User.class)
//    User SearchStaffByID(@Param("id") Integer id);

    //根据姓名对员工的模糊查询
//    @Select("select * from user where Name like #{name}")
//    @ResultType(User.class)
//    List<User> SearchStaffByName(@Param("name") String name);

    //超管更改管理员、员工\管理员更改员工信息（姓名、性别、部门、权限、状态）
//    @Update("update user set Name = #{name}, Sex = #{sex}, Department = #{department}, Permission = #{permission}, State = #{state} where User_Id = #{userId}")
//    Integer AdminChangeStaffState(User user);

//    //查询部门员工
//    @Select("select * from user where Department = #{dname}")
//    @ResultType(User.class)
//    List<User> SearchStaffByDepartment(@Param("dname") String dname);
//
//    //个人信息管理（包括超管、管理员、员工），只能修改密码，邮箱。手机号
//    @Update("update user set PhoneNumber = #{phoneNumber}, Password = #{password}, Email = #{email} where User_Id = #{userId}")
//    Integer UpdateStaffs(User user);
//
//    //修改密码
//    @Update("update user set Password = #{password} where User_Id = #{userId}")
//    Integer StaffUpdatePwd(@Param("userId") Integer userId, @Param("password") String password);



    /**
     * 超管查询全部用户
     * @return List<User> userList
     */
    @Select("SELECT * FROM user WHERE User_Id <> #{Id}")
    @ResultType(User.class)
    List<User> ShowAll(@Param("Id") Integer Id);

    /**
     * 检查该部门是否已设置管理员
     * @param department
     * @return Integer
     */
    @Select("SELECT * FROM user WHERE Department = #{department} AND Permission = #{permission}")
    List<User> AdminExist(@Param("department") String department, @Param("permission") String permission);

    @Select("Select * from user where User_Number = #{userNumber}")
    Integer SelectUserByUserNumber(@Param("userNumber")String userNumber);

    //批量的在UserMapper，简单的直接注解操作
    //批量添加员工（XML配置）
    Integer AddStaffsBatch(@Param("users") List<User> users);

    @SelectKey(statement = "select last_insert_id()", before = false, keyProperty = "user.userId", resultType = Integer.class)
    @Insert("INSERT INTO user(User_Number,Name,Sex,PhoneNumber,Password,Email,Department,Permission,State)" +
            " VALUES(#{user.userNumber},#{user.name},#{user.sex},#{user.phoneNumber},#{user.password}," +
            "#{user.email},#{user.department},#{user.permission},#{user.state})")
    Integer AddUser(@Param("user") User user);

    //批量删除员工（XML配置）
    Integer DeleteStaffsBatch(@Param("ids") List<Integer> ids);

    //根据User_Id单个删除员工
    @Delete("DELETE FROM user WHERE User_Id = #{id}")
    Integer DeleteUser(@Param("id") Integer id);

    //批量删除员工，前端传入的ids[]数组
    Integer DeleteUserBatch(@Param("ids") Integer[] ids);

    //根据ID查找员工
    @Select("select * from user where User_Id = #{id}")
    @ResultType(User.class)
    User SearchStaffByID(@Param("id") Integer id);

    //根据姓名对员工的模糊查询
    @Select("select * from user where Name like #{name} and Department like #{Department}")
    @ResultType(User.class)
    List<User> SearchStaffByName(@Param("name") String name,@Param("Department")String Department);

    //通过邮箱查找员工返回User类数据
    @Select("select * from user where Email = #{email}")
    @ResultType(User.class)
    User SearchStaffByEmail(@Param("email") String email);


    //超管更改管理员、员工\管理员更改员工信息（姓名、性别、部门、权限、状态）
    @Update("update user set Name = #{name}, Sex = #{sex}, Department = #{department}, Permission = #{permission}, State = #{state} where User_Id = #{userId}")
    Integer AdminChangeStaffState(User user);

    //超管修改员工信息，全权限修改
    @Update("UPDATE user SET User_Number = #{userNumber}, Name=#{name},Sex=#{sex},PhoneNumber=#{phoneNumber}," +
            "Password=#{password},Email=#{email},Department=#{department},Permission=#{permission},State=#{state} " +
            "WHERE User_Id=#{userId}")
    Integer SAdminModify(User user);

    //超管重置密码
    @Update("UPDATE user SET Password = #{DefaultPassword} ")
    Integer ResetAllPassword(@Param("DefaultPassword") String DefaultPassword);

    //查询部门员工
    @Select("select * from user where Department = #{dname}")
    @ResultType(User.class)
    List<User> SearchStaffByDepartment(@Param("dname") String dname);

    //个人信息管理（包括超管、管理员、员工），只能修改密码，邮箱。手机号
    @Update("update user set PhoneNumber = #{phoneNumber}, Email = #{email} where User_Id = #{userId}")
    Integer UpdateStaffs(UserVO userVO);

    //修改密码
    @Update("update user set Password = #{password} where User_Id = #{userId}")
    Integer StaffUpdatePwd(@Param("userId") Integer userId, @Param("password") String password);

    //批量允许用户使用云盘
    Integer AllowState(@Param("Ids") List<Integer> Ids);

    //批量禁止用户使用云盘
    Integer BanState(@Param("Ids") List<Integer> Ids);

    //通过部门获取该部门的管理员实例
    @Select("SELECT * FROM user WHERE Department = #{Department} AND Permission = \"管理员\"")
    User GetDepartmentAdmin(@Param("Department") String Department);

    @Update("UPDATE user SET Permission = \"员工\" WHERE User_Id = #{userId}")
    Integer Demotion(@Param("userId") Integer userId);

    @Update("UPDATE user SET Permission = \"管理员\" WHERE User_Id = #{userId}")
    Integer Promoted(@Param("userId") Integer userId);

    //根据工号查询员工信息
    @Select("select * from user where User_Number = #{userNumber}")
    @ResultType(User.class)
    User SearchUserByNumber(String userNumber);

}
